var express = require("express");
var router = express.Router();

const axios = require("axios");

var map = require("../data/map");
var data = require("../data/data");
var data2 = require("../data/data2");
var changeData = require("../data/change");

router.get("/map/json", function (req, res, next) {
  res.json(map);
});

router.get("/gdp/json", function (req, res, next) {
  res.json({
    data,
  });
});

router.get("/gdp2/json", function (req, res, next) {
  res.json({
    data: data2,
  });
});

let time = 0;
let result = {};
router.get("/weather/json", function (req, res, next) {
  const { timestamp } = req.query;

  if (timestamp) {
    if (timestamp - time > 1000 * 60 * 5) {
      axios
        .get(
          "https://sapi.k780.com/?app=weather.today&weaId=94&appkey=74968&sign=3ef55348456d4d1aa764a2daee44756e&format=json"
        )
        .then((response) => {
          time = timestamp;
          result = response.data.result;
          res.json(response.data.result);
        });
    } else {
      res.json(result);
    }
  } else {
    res.json({
      msg: "参数错误",
    });
  }
});

router.get("/gdpchange/json", function (req, res, next) {
  res.json({
    data: changeData,
  });
});

module.exports = router;
